package com.ruoyi.prescription.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.prescription.domain.Prescription;
import com.ruoyi.prescription.service.IPrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 处方Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/prescription/prescription")
public class PrescriptionController extends BaseController {
    @Autowired
    private IPrescriptionService prescriptionService;

    /**
     * 查询处方列表
     */
    @PreAuthorize("@ss.hasPermi('prescription:prescription:list')")
    @GetMapping("/list")
    public TableDataInfo list(Prescription prescription) {
        startPage();
        List<Prescription> list = prescriptionService.selectPrescriptionList(prescription);
        return getDataTable(list);
    }

    /**
     * 导出处方列表
     */
    @PreAuthorize("@ss.hasPermi('prescription:prescription:export')")
    @Log(title = "处方", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Prescription prescription) {
        List<Prescription> list = prescriptionService.selectPrescriptionList(prescription);
        ExcelUtil<Prescription> util = new ExcelUtil<Prescription>(Prescription.class);
        util.exportExcel(response, list, "处方数据");
    }

    /**
     * 获取处方详细信息
     */
    @PreAuthorize("@ss.hasPermi('prescription:prescription:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(prescriptionService.selectPrescriptionById(id));
    }

    /**
     * 新增处方
     */
    @PreAuthorize("@ss.hasPermi('prescription:prescription:add')")
    @Log(title = "处方", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prescription prescription) {
        return toAjax(prescriptionService.insertPrescription(prescription));
    }

    /**
     * 修改处方
     */
    @PreAuthorize("@ss.hasPermi('prescription:prescription:edit')")
    @Log(title = "处方", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prescription prescription) {
        return toAjax(prescriptionService.updatePrescription(prescription));
    }

    /**
     * 删除处方
     */
    @PreAuthorize("@ss.hasPermi('prescription:prescription:remove')")
    @Log(title = "处方", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(prescriptionService.deletePrescriptionByIds(ids));
    }

    /**
     * 提醒患者用药
     */
    @PreAuthorize("@ss.hasPermi('prescription:prescription:remind')")
    @Log(title = "处方", businessType = BusinessType.OTHER)
    @PostMapping("/remind/{id}")
    public AjaxResult remindMedication(@PathVariable("id") Long id) {
        return toAjax(prescriptionService.remindMedication(id));
    }

    /**
     * 获取未读用药提醒
     */
    @GetMapping("/unreadReminders")
    public AjaxResult getUnreadReminders() {
        return success(prescriptionService.getUnreadReminders());
    }
}
